<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="mall.demo.util.SessionKeyEnum" %>
<%@ page import="mall.demo.mybatis.pojo.ProductInCart" %>
<%@ page import="mall.demo.mybatis.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%!
	int calcCartNumber(List<ProductInCart> productInCart) {
		if(productInCart == null || productInCart.isEmpty()) {
			return 0;
		}
		int sum = 0;
		for(ProductInCart pc : productInCart) {
			sum += pc.getAmount();
		}
		return sum;
	}

%>
<%

Object obj = session.getAttribute(SessionKeyEnum.userObj.name());
boolean isUserLoggedIn = false;
if(obj != null) {
	isUserLoggedIn = true;
}
User userInfo = null;
if (isUserLoggedIn) {
	userInfo = (User)obj;
}
List<ProductInCart> productInCart = new ArrayList<ProductInCart>();
if (isUserLoggedIn) {
	productInCart = (List<ProductInCart>)session.getAttribute(SessionKeyEnum.cart.name());
	if (productInCart == null) {
		productInCart = new ArrayList<ProductInCart>();
	}
}
int number = 0;
for(ProductInCart p:productInCart){
	number += p.getAmount();
}
%>
<div class="top">
<!-- <ul class="nav nav-pills pull-right">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
        </ul> --> 
<ul class="nav nav-pills pull-right">
<% if(isUserLoggedIn) {%>
<li><a href="#">欢迎 <%=userInfo.getDisplayName() %></a></li>
<li><a href="cart.jsp">购物车(<span id="cartnumber"><%=calcCartNumber(productInCart) %></span>)</a></li>
<li><a href="order.jsp">我的订单</a></li>
<li><a href="servlet/logout.do">Logout</a></li>

<% } else { %>
<li><a href="register.jsp">注册</a></li>
<li><a href="login.jsp">登录</a></li>
<%} %>
</ul>
</div>